package com.wlw.admin.openeye.ui.activity

import android.graphics.Color
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.bumptech.glide.Glide
import com.wlw.admin.openeye.Constants
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.base.BaseActivity
import com.wlw.admin.openeye.mvp.contract.CategoryDetailContract
import com.wlw.admin.openeye.mvp.model.bean.CategoryBean
import com.wlw.admin.openeye.mvp.model.bean.HomeBean
import com.wlw.admin.openeye.mvp.presenter.CategoryDetailPresenter
import com.wlw.admin.openeye.ui.adapter.CategoryDetailAdapter
import com.wlw.admin.openeye.utils.StatusBarUtil
import kotlinx.android.synthetic.main.activity_category_detail.*

class CategoryDetailActivity : BaseActivity(), CategoryDetailContract.View {

    private val mPresenter by lazy {
        CategoryDetailPresenter()
    }
    private var categoryData: CategoryBean? = null

    private var itemList = ArrayList<HomeBean.Issue.Item>()

    private val mAdapter by lazy {
        CategoryDetailAdapter(this, itemList)
    }

    init {
        mPresenter.attachView(this)
    }

    private var loadingMore = false

    override fun layoutId(): Int = R.layout.activity_category_detail

    override fun initData() {
        categoryData = intent.getSerializableExtra(Constants.BUNDLE_CATEGORY_DATA) as CategoryBean
    }

    override fun initView() {
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener { finish() }

        Glide.with(this).load(categoryData?.headerImage)
                .into(imageView)
        tv_category_desc.text = "#${categoryData?.description}#"
        collapsing_toolbar_layout.title = categoryData?.name
        collapsing_toolbar_layout.setExpandedTitleColor(Color.WHITE)
        collapsing_toolbar_layout.setCollapsedTitleTextColor(Color.BLACK)

        mRecyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerView.adapter = mAdapter
        mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                val item = recyclerView.layoutManager?.itemCount
                val lastVisibleItem = (recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
                if (!loadingMore && lastVisibleItem == (item?.minus(1))) {
                    loadingMore = true
                    mPresenter.loadMoreData()
                }
            }
        })
        StatusBarUtil.darkMode(this)
        StatusBarUtil.setPaddingSmart(this, toolbar)
    }

    override fun start() {
        categoryData?.id?.let { mPresenter.getCategoryDetailList(it) }
    }

    override fun setCateDetailList(itemList: ArrayList<HomeBean.Issue.Item>) {
        loadingMore = false
        mAdapter.addData(itemList)
    }

    override fun showError(errorMsg: String) {
        multipleStatusView.showError()
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }

    override fun showLoading() {
    }

    override fun dismissLoading() {
    }


}
